package cn.umakr.dao.Impl;

import cn.umakr.dao.UserInfoDao;
import cn.umakr.entity.User;
import cn.umakr.entity.UserInfo;
import cn.umakr.util.DbHelper;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangbo on 2017/7/17.
 */
public class UserInfoDaoImpl implements UserInfoDao {

    //实例化一个QueryRunner对象用于执行sql语句，执行sql语句时需要提供connection对象
    private QueryRunner queryRunner = new QueryRunner();

    /**
     * 增加一个UserInfo对象
     *
     * @param userInfo
     */
    @Override
    public Integer save(UserInfo userInfo) {

        Integer result = 0;
        try {
            String sql = "insert into tab_userinfo values(null,?,?,?,?,?,?,?,?,?,?,?,?)";
            Connection connection = DbHelper.getConn();
            result = queryRunner.update(connection, sql, userInfo.getName(), userInfo.getSex(), userInfo.getQq(),
                    userInfo.getBdate(), userInfo.getCdate(), userInfo.getFace(), userInfo
                            .getIntroduce());
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除一个UserInfo对象,根据主键删除
     *
     * @param id
     */
    @Override
    public Integer delete(Integer id) {

        Integer result = 0;
        try {
            String sql = "delete from tab_userinfo where id = ?";
            Connection connection = DbHelper.getConn();
            result = queryRunner.update(connection, sql, id);
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 修改UserInfo对象
     *
     * @param userInfo
     */
    @Override
    public Integer update(UserInfo userInfo) {
        Integer result = 0;
        try {
            String sql = "update tab_userinfo set name=?,sex=?,qq=?,email=?,phone=?,provice=?,pid=?,uid=?,bdate=?," +
                    "cdate=?,face=?,introduce=? where id = ?";
            Connection connection = DbHelper.getConn();
            result = queryRunner.update(connection, sql, userInfo.getName(), userInfo.getSex(), userInfo.getQq(),
                    userInfo.getEmail(), userInfo.getPhone(), userInfo.getProvice(), userInfo.getPid(), userInfo
                            .getUid(), userInfo.getBdate(), userInfo.getId());
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 查询所有UserInfo对象
     */
    @Override
    public List<UserInfo> findAll() {
        List<UserInfo> userInfos = new ArrayList<UserInfo>();
        try {
            String sql = "select * from tab_userinfo";
            BeanListHandler<UserInfo> rsh = new BeanListHandler<UserInfo>(UserInfo.class);
            Connection connection = DbHelper.getConn();
            userInfos = queryRunner.query(connection, sql, rsh);
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userInfos;
    }

    /**
     * 根据主键查找UserInfo对象
     *
     * @param id
     */
    @Override
    public UserInfo findById(Integer id) {

        UserInfo userInfo = null;
        try {
            String sql = "select * from tab_userinfo where id = ?";
            BeanHandler<UserInfo> rsh = new BeanHandler<UserInfo>(UserInfo.class);
            Connection connection = DbHelper.getConn();
            userInfo = queryRunner.query(connection, sql, rsh, id);
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userInfo;
    }

    /**
     * 根据属性查找UserInfo对象
     *
     * @param propName
     * @param propValue
     */
    @Override
    public List<UserInfo> findByProperty(String propName, Object propValue) {
        List<UserInfo> userInfos = new ArrayList<UserInfo>();
        try {
            String sql = "select * from tab_userinfo where " + propName + "=?";
            Connection connection = DbHelper.getConn();
            BeanListHandler<UserInfo> rsh = new BeanListHandler<UserInfo>(UserInfo.class);
            userInfos = queryRunner.query(connection, sql, rsh, propValue);
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userInfos;
    }
}
